<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Session;
use think\Db;
class Article extends Common
{

    public function index()
    {

    }
    public function a_list(){

        $res = Db::table('article')
            -> alias('a')
            -> join('article_type at','a.article_type_id = at.article_type_id')-> paginate(10);
        $this->assign('res',$res);
        return $this -> fetch();
    }
    /*
     * 添加文章
     * */
    public function a_add(){
        $data = Request::instance() -> Param();
        if($data['article_id']){
            $res = Db::table('article') ->where($data) -> find();
            $this->assign('res',$res);
        }
        $a_type = Db::table('article_type') -> select();
        $this->assign('a_type',$a_type);
        return $this->fetch();
    }
    public function doa_add(){
        $session = session::get('user');
        $data = Request::instance()-> Param();
        if($data['article_id']){
            $result = Db::table('article') -> where('article_id',$data['article_id']) -> update($data);
            if($result){
                return(array(
                    'error'  => 0,
                    'msg'    => '更新成功',
                    'url'    => '/admin/article/a_list'
                ));
            }else{
                return(array(
                    'error'  => 0,
                    'msg'    => '更新失败'
                ));
            }
        }
        $data['article_addtime'] = time();
        $data['article_author'] = $session['name'];
        $result = Db::table('article') -> insert($data);
        if($result){
            return(array(
                'error'  => 0,
                'msg'    => '添加成功',
                'url'    => '/admin/article/a_list'
            ));
        }else{
            return(array(
                'error'  => 0,
                'msg'    => '添加失败'
            ));
        }
    }
    /*
     * 添加文章分类
     * */
    public function a_type(){

        $res = Db::table('article_type')-> paginate(10);
        $this->assign('res',$res);
        return $this -> fetch();
    }
    public function at_add(){
        $data = Request::instance() -> Param();
        $res = Db::table('article_type') -> where($data) -> find();

        $this->assign('res',$res);
        return $this->fetch();
    }
    public function doat_add(){
        $param = Request::instance()->Param();
        if($param['article_type_id']){
            $result = Db::table('article_type') -> where('article_type_id',$param['article_type_id']) -> update($param);
            if($result){
                return(array(
                    'error' => 0,
                    'msg'   => '更新成功',
                    'url'   => '/admin/Article/a_type'
                ));
            }else{
                return(array(
                    'error' => 1,
                    'msg'   => '更新失败',
                    'url'   => '/admin/Article/at_add'
                ));
            }
        }
        $data = $param;
        unset($data['article_type_id']);
        $data['article_type_time'] = time();
        $result = Db::table('article_type') -> insert($data);
        if($result){
            return(array(
                'error' => 0,
                'msg'   => '添加成功',
                'url'   => '/admin/Article/a_type'
            ));
        }else{
            return(array(
                'error' => 1,
                'msg'   => '添加失败',
                'url'   => '/admin/Article/at_add'
            ));
        }
    }
    /*
     * 删除文章
     * */
    public function a_delete(){
        $data = Request::instance() -> Param();
        $a = Db::table('article') -> where($data) -> select();
        if($a){
            Db::table('article') -> where($data) -> delete();
        }
        $result = Db::table('article_type') -> where($data) -> delete();
        if($result){
            return(array(
                'error'  => 0,
                'msg'    => '删除成功',
                'url'    => '/admin/Article/a_type'
            ));
        }else{
            return(array(
                'error'  => 1,
                'msg'    => '删除成功',
                'url'    => '/admin/Article/a_type'
            ));
        }
    }
}
